package com.gjy.quartz.trigger;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-08-30 15:57:13
 */
public class CronTriggerTest {

    public static void main(String[] args) throws Exception {
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        Map<String, String> map = new HashMap<>();
        map.put("name", "simple");
        map.put("time", LocalDateTime.now().toString());

        JobDetail job = JobBuilder.newJob(TriggerJob.class)
                .withIdentity("cron", "triggerJob")
                .usingJobData(new JobDataMap(map))
                .build();

        String cron = "0/10 * * * * ?";
        if (!CronExpression.isValidExpression(cron)) {
            throw new IllegalAccessException("cron错误: " + cron);
        }

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("cron", "triggerTrigger")
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                .build();

        scheduler.scheduleJob(job, trigger);

        scheduler.start();
    }
}
